package com.wondersgroup.retrofittest;

import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by Administrator on 2017/1/24.
 */

public class RecyclerStringAdapter extends RecyclerView.Adapter<TextViewHolder> {
    SparseArray<String> sparseArray;
    @Override
    public TextViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new TextViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_text,parent,false));
    }

    @Override
    public void onBindViewHolder(TextViewHolder holder, int position) {
        holder.tv_name.setText(sparseArray.get(position));
    }

    @Override
    public int getItemCount() {
        return sparseArray == null ? 0 : sparseArray.size();
    }

    public RecyclerStringAdapter() {
    }

    public void setData(SparseArray<String> arr){
        if (sparseArray != null){
            sparseArray.clear();
        }
        sparseArray = arr;
    }
    public void setData(List<String> list) {
        if (sparseArray == null){
            sparseArray = new SparseArray<>();
        }else {
            sparseArray.clear();
        }

        for (int i=0,size = list.size(); i<size; i++){
            sparseArray.append(i,list.get(i));
        }
        notifyDataSetChanged();
    }

    public void setData(String[] arr) {
        if (sparseArray == null){
            sparseArray = new SparseArray<>();
        }else {
            sparseArray.clear();
        }
        if (arr != null && arr.length > 0) {
            for (int i = 0, size = arr.length; i < size; i++) {
                this.sparseArray.append(i,arr[i]);
            }
        }
        notifyDataSetChanged();
    }

    public void addItem(String str){
        if (sparseArray == null){
            sparseArray = new SparseArray<>();
        }
        sparseArray.append(sparseArray.size(),str);
    }
}
